package com.dal.config;

import com.dal.plugin.DBSavePlugin;
import com.jfinal.config.*;
import com.jfinal.json.FastJsonFactory;
import com.jfinal.kit.PathKit;
import com.jfinal.template.Engine;
import com.sojoys.artifact.constant.ConfigConstant;
import com.sojoys.artifact.manager.ContextManager;
import com.sojoys.artifact.manager.PropManager;
import com.sojoys.artifact.manager.ThreadManager;
import com.sojoys.artifact.plugin.quartz.QuartzPlugin;
import com.sojoys.artifact.plugin.syncdatabase.SyncMysqlPlugin;
import org.apache.activemq.jms.pool.PooledConnectionFactory;
import org.cache.InterceptMappingCache;
import org.springframework.jms.connection.SingleConnectionFactory;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import org.utils.RemoteUtil;

/**
 * @author : DengYing
 * @CreateDate : 2017年4月10日 下午5:00:59
 * @Description ：Please describe this document
 */
public class PayConfig extends JFinalConfig {

	@Override
	public void configConstant(Constants constants) {
		// 设置配置文件
		PropManager.use("app.properties");

		// xml配置文件
		ContextManager.me().init("app-init.xml");
		
		// 字符编码
		constants.setEncoding("UTF-8");
		
		// 开发模式
		constants.setDevMode(PropManager.getBoolean(ConfigConstant.CONFIG_DEVMODE, false));
		
		// 上传公共路径 + File.separator + "files"
		constants.setBaseUploadPath(PathKit.getWebRootPath());
		
		// 下载公共路径 + File.separator + "files"
		constants.setBaseDownloadPath(PathKit.getWebRootPath());
		
		// 设置默认JSON工厂
		constants.setJsonFactory(FastJsonFactory.me());
	}

	@Override
	public void configRoute(Routes routes) {
		routes.add(new RoutesScan());
	}

	@Override
	public void configEngine(Engine me) {

	}

	@Override
	public void configPlugin(Plugins me) {
		me.add(new SyncMysqlPlugin());
		me.add(new QuartzPlugin());
		
		me.add(new DBSavePlugin());
		
//		me.add(new RmiPlugin().addRmiServer(LoginRMIServerImpl.me()));
	}

	@Override
	public void configInterceptor(Interceptors me) {
	}

	@Override
	public void configHandler(Handlers me) {

	}

	@Override
	public void afterJFinalStart() {
//		RemoteUtil.gameTopic().startLoginServer();

		startActiveMQ();
	}

	@Override
	public void beforeJFinalStop() {
		ThreadManager.me().stop();
		stopAcriveMQ();
	}

	private void startActiveMQ(){
//		DefaultMessageListenerContainer topicListenerContainer = ContextManager.me().getBean("topicListenerContainer", DefaultMessageListenerContainer.class);
//		topicListenerContainer.start();
		
		DefaultMessageListenerContainer queueListenerContainer = ContextManager.me().getBean("queueListenerContainer", DefaultMessageListenerContainer.class);
		queueListenerContainer.start();
	}
	
	private void stopAcriveMQ() {
//		DefaultMessageListenerContainer topicListenerContainer = ContextManager.getBean("topicListenerContainer", DefaultMessageListenerContainer.class);
//		topicListenerContainer.destroy();
		
		DefaultMessageListenerContainer queueListenerContainer = ContextManager.me().getBean("queueListenerContainer", DefaultMessageListenerContainer.class);
		queueListenerContainer.destroy();
		
		PooledConnectionFactory pooledConnectionFactory = ContextManager.me().getBean("pooledConnectionFactory", PooledConnectionFactory.class);
		pooledConnectionFactory.stop();
		
		SingleConnectionFactory singleConnectionFactory = ContextManager.me().getBean("singleConnectionFactory", SingleConnectionFactory.class);
		singleConnectionFactory.destroy();
	}
	
}
